package model.trader.trade;
//package model.trader.strategy;
//
//import model.market.BankOfCredit;
//import model.market.MarketManager;
//import model.market.Position;
//import model.market.Trade;
//import model.trader.MomentumTrader;
//import model.trader.Trader;
//
//public class AssetValueTrade {
//
//	//AM: 1/FRACTIONAL_BET_PER_PERIOD of the working capital is invested each period the asset is mispriced.
//	private static final int FRACTIONAL_BET_PER_PERIOD = 12;
//	
//	private double BET_LIMIT_BUFFER = .6;
//	
//	//AMXX: posibly. private static final double GOOD_DEAL_PRICE_BUFFER = 1.03;
//	
//	private double MARGIN_BUFFER=.3;//Keep some money to avoid margin call.
//	//AMXX: possibly private double softToleranceSpread = .07;
//	
//	
//	/**
//	 * 
//	 * @param trader 
//	 * @param market
//	 * @param position - the trader's position
//	 * @param fairPrice - the fair price as determined by the trader.
//	 * @param hardTolerance - the difference between the fairprice and market price at which trader feels strongly that
//	 * 						action must be taken. expressed as percentage, e.g 50%
//	 * @param shortMargin - margin required in excess of cash value of stock shorted, e.g .1
//	 * @param longMargin - margin required when leveraged purchase of stock, e.g .1
//	 */
//	//AMXX: instead of determining how much to trade each period, think in terms of how much you want to hold.
//	public void determineTrade(Trader trader, MarketManager market, 
//							Position position, 
//							double fairPrice) {
//		
//		int realQuantity = position.getExecutedQuantity();
//		int absoluteQuantity = Math.abs(position.getExecutedQuantity());
//		
//		//sell it hard..
//		if(market.getMarketPrice()>fairPrice){
//			double workingCapitalRiskLimit = new BankOfCredit().determineWorkingCapitalShort(market, position, MARGIN_BUFFER);
//			double workingCapitalInvestLimit = new BankOfCredit().determineWorkingCapitalShort(market, position, BET_LIMIT_BUFFER);
//			
//			
////			if(market.getMarketPrice()>fairPrice*(1+hardTolerance)){
////				//if we still got a long pos, close it pronto!...
////				if(realQuantity>0){
////					market.closeOrder(position);
////				}else if(workingCapital>0){
////					//short it!!
////					market.order( 
////						new Trade(trader,(int)(workingCapital/3/market.getMarketPrice()),Trade.SELL,fairPrice*GOOD_DEAL_PRICE_BUFFER));
////				}
////				
////			}else {
//				//soft value ..
//				//AMXX: possibly .double goodDealPrice = market.getMarketPrice()*GOOD_DEAL_PRICE_BUFFER;
//				//if we have a long pos,  close it, but try to get a good price...
//				if(realQuantity>0){
//					//close..
//					market.order(new Trade(trader,absoluteQuantity,Trade.SELL,fairPrice));
//				}else if(workingCapitalInvestLimit>0){
//					//sell a little bit..
////					market.order(
////							new Trade(trader,Trade.PASSIVE,
////									(int)(workingCapital/WORKING_CAP_SPLIT_SUB_HARD_TOLERANCE/market.getMarketPrice()),
////									goodDealPrice,
////									Trade.SELL));		
//					//if(market.getMarketPrice()>fairPrice*(1+softToleranceSpread)){
//						//sell a little bit aggressively..
//						double valueToShort = (workingCapitalRiskLimit/FRACTIONAL_BET_PER_PERIOD)*market.getMarketPrice()/fairPrice;
//						
//						market.order(
//								new Trade(trader,
//										(int)(valueToShort/market.getMarketPrice()),
//										Trade.SELL,fairPrice));
//					//}
//				}else if (workingCapitalRiskLimit < 0) {
//					//Stop loss.. start trading out..
////					AM: handle stop loss, tighten up if working capital <0, etc.??
//					loosenRisk(trader, market, position);
//				}
////			}
//		}else if(market.getMarketPrice()<fairPrice){
//		
//			double workingCapitalRiskLimit = new BankOfCredit().determineWorkingCapitalLong(market, position,MARGIN_BUFFER);
//			double workingCapitalInvestLimit = new BankOfCredit().determineWorkingCapitalLong(market, position,BET_LIMIT_BUFFER);
//			
////			if(workingCapital<0&&position.getExecutedQuantity()>0){
////				//this is really our stop loss measure, we need to close down our position..
////				market.order(new Trade());
////			}
//			
//			// buy it hard..
////			if (market.getMarketPrice() < fairPrice * (1 / (1 + hardTolerance))) {
////				
////
////				// if we still got a short pos, close it pronto!...
////				if (realQuantity < 0) {
////					market.closeOrder(position);
////
////				} else if (workingCapital > 0) {
////					// Lever up!!
////					market
////							.order(new Trade(trader,
////									(int) (workingCapital / 3 / market
////											.getMarketPrice()), Trade.BUY,
////									fairPrice * (1 / GOOD_DEAL_PRICE_BUFFER)));
////				}
////
////			} else {
//				
//
////	AMXX: possibly			double goodDealPrice = market.getMarketPrice()
////						* (1 / GOOD_DEAL_PRICE_BUFFER);
//				// if we have a short pos, close it, but try to get a good
//				// price...
//				if (realQuantity < 0) {
//					// close..
//					market.order(new Trade(trader, absoluteQuantity, Trade.BUY,
//							fairPrice ));
//
//				}else if (workingCapitalInvestLimit > 0) {
//					// buy a little bit..
////					market.order(new Trade(
////									trader,
////									Trade.PASSIVE,
////									(int) (workingCapital
////											/ WORKING_CAP_SPLIT_SUB_HARD_TOLERANCE / market
////											.getMarketPrice()), goodDealPrice,
////									Trade.BUY));
////					if (market.getMarketPrice() < fairPrice
////							* (1 / (1 + softToleranceSpread))) {
//						// buy a little bit aggresively..
//						double valueToBuy = (workingCapitalRiskLimit / FRACTIONAL_BET_PER_PERIOD)
//								* fairPrice / market.getMarketPrice();
//
//						market.order(new Trade(trader,
//								(int) (valueToBuy / market.getMarketPrice()),
//								Trade.BUY, fairPrice));
////					}
//
//				}else if (workingCapitalRiskLimit < 0) {
//					//Stop loss.. start trading out..
//					loosenRisk(trader, market, position);
//				}
//
//			}
////		}
//	}
//	
//	private void loosenRisk(Trader trader, MarketManager market, Position position) {
//		int quantity = position.getExecutedQuantity();
//		boolean direction = Trade.SELL;
//		if(quantity<0){
//			direction=Trade.BUY;
//		}
//		Trade cancel = new Trade(trader,
//							Math.abs(quantity/4)+1,direction);
//		market.order(cancel);
//	}
//
//	
//	
//	
//}
